package net.easyjava.test.aws;

import com.alibaba.fastjson.JSONObject;
import com.amazonaws.services.sqs.model.Message;
import net.easyjava.tools.security.encrypt.MD5Tools;

import java.util.List;

/**
 * Created by wxnacy on 16/11/24.
 */
public class User1Demo {
    public static final String TOPIC_ARN1 = "arn:aws:sns:us-east-1:053227996260:topic-1";
    public static final String QUEUE_2 = "queue-2";
    public static final String QUEUE_URL_2 = "https://sqs.us-east-1.amazonaws.com/053227996260/queue-2";


    public static void main(String[] args) {
        String md5 = MD5Tools.getInstance().encryptToString(System.currentTimeMillis()+"");
        System.out.println("向User2发送上传秘钥开始");
        System.out.println("本次秘钥:       "+md5);
        SQSDemo sqsDemo = new SQSDemo();
        S3Demo s3Demo = new S3Demo();
        SNSDemo snsDemo = new SNSDemo();

        snsDemo.sendMessageToTopic(TOPIC_ARN1,md5);

        while(true){
            List<Message> messages = sqsDemo.receiveMessageFromTopic(QUEUE_URL_2);
            if(messages.size()>0 && messages.get(0) !=null) {
                Message message = messages.get(0);

                System.out.println("接收到上传文件");
                System.out.println("地址:         "+ JSONObject.parseObject(message.getBody()).get("Message"));
                System.out.println("准备发送下个文件秘钥");
                md5 = MD5Tools.getInstance().encryptToString(System.currentTimeMillis()+"");
                snsDemo.sendMessageToTopic(TOPIC_ARN1,md5);
                System.out.println("发送成功:       " + md5);
                System.out.println("等待接收文件地址");

                try {
                    sqsDemo.delMsg(QUEUE_URL_2,message.getReceiptHandle());
                    Thread.sleep(200);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }



    }



}
